<?php
class Mozg_Subactions
{
	private $_subActions;
    private static $_instance;

    protected function __construct (){}
    
    static public function GetInstance ()
    {
        if (! (self::$_instance instanceof self)) self::$_instance = new self();
        return self::$_instance;
    }	
	
    public function Add($key, $callBack, array $params = array(), $subkey = false)
    {
        if (!$key || !is_string($key)) return false;
        
        if (!isset($this->_subActions[$key]) || !is_array($this->_subActions[$key]))
        {
        	$this->_subActions[$key] = array();
        }
        
        if (false === $subkey)
        {
        	$this->_subActions[$key][] = array('callback' => $callBack , 'params' => $params);
        }
        else
        {
        	$this->_subActions[$key][$subkey] = array('callback' => $callBack , 'params' => $params);
        }
        
        return $this;
    }    
    
    public function Set($key, $callBack, array $params = array(), $subkey = false)
    {
        if (!$key || !is_string($key)) return false;
        
  		$this->_subActions[$key] = array();	
        $this->Add($key, $callBack, $params, $subkey);
        return $this;
    }
    
    public function Is($key)
    {
    	return isset($this->_subActions[$key]);
    }
    
    public function Get($key)
    {
        if (!$this->Is($key)) return array();

        $data = array();
        foreach ($this->_subActions[$key] as $secondAction)
        {
        	$data[] = call_user_func_array($secondAction['callback'], $secondAction['params']);
        }	
        
        unset($this->_subActions[$key]);
        
        return $data;
    }
}